package certificate.model;

import certificate.entity.Course;
import certificate.entity.StudentSubjectStudy;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;



public class MarkModel {
    
    private String studentId;
    
    private String studentName;
    
    private CourseModel cbbModel;
    
    private SubjectStudentModel tblMark;
    
    

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public SubjectStudentModel getTblMark() {
        return tblMark;
    }

    public void setTblMark(SubjectStudentModel tblMark) {
        this.tblMark = tblMark;
    }
    
    public void setTblMark(List<StudentSubjectStudy> list){
        tblMark = new SubjectStudentModel(list);
    }
    
    public CourseModel getCbbModel() {
        return cbbModel;
    }

    public void setCbbModel(CourseModel cbbModel) {
        this.cbbModel = cbbModel;
    }

    public void setCbbModel(List<Course> list){
        this.cbbModel = new CourseModel(list);
    }
    
    public class CourseModel extends AbstractListModel implements ComboBoxModel {
        
        private List<Course> list;
        
        private int selectedIndex;

        public CourseModel(List<Course> list) {
            this.list = list;
            
            selectedIndex = 0;
        }

        public CourseModel() {
        }
        
        @Override
        public void setSelectedItem(Object anItem) {
            String name =  (String) anItem;
            for(int i = 0; i < list.size(); i++){
                if(list.get(i).getName().equals(name)) {
                    selectedIndex = i;
                    break;
                }
            }
        }

        @Override
        public Object getSelectedItem() {
            return list.get(selectedIndex).getName();
        }
        
        public Course getSelectedCourse(){
            return list.get(selectedIndex);
        }

        @Override
        public int getSize() {
            return list.size();
        }

        @Override
        public Object getElementAt(int index) {
            return list.get(index).getName();
        }

        @Override
        public void addListDataListener(ListDataListener l) {
            
        }

        @Override
        public void removeListDataListener(ListDataListener l) {
            
        }
    }
    
    
    public class SubjectStudentModel extends AbstractTableModel {
    
        // Model of display Data
        private List<StudentSubjectStudy> lstSubjectStudent = null;
        
        // Model of Updating Data
        private Set<Integer> indexChanges;
        
        // Model of Rederer row Data (colour of row)
        private List<Color> rowColours = null;
        
        private String[] columnName = {"Subject ID", "Subject Name", "Mark"};
        
        private Class[] colType = {String.class, String.class, Integer.class};

        public SubjectStudentModel() {
            this.indexChanges = new HashSet<Integer>();
            this.rowColours = new ArrayList<Color>();
            addTableModelListener(new TableModelListener() {
                @Override
                public void tableChanged(TableModelEvent e) {
                    int firstRow = e.getFirstRow();
                    int lastRow =  e.getLastRow();
                    
                    System.out.println("firstRow : " + firstRow);
                    System.out.println("lastRow : " + lastRow);
                }
            });
        }

        public SubjectStudentModel(List<StudentSubjectStudy> lstSubject) {
            this();
            this.lstSubjectStudent = lstSubject;
            
            for(int i = 0; i < lstSubject.size(); i++){
                rowColours.add(Color.white);
            }
        }

        public List<StudentSubjectStudy> getLstSubjectStudent() {
            return lstSubjectStudent;
        }

        public void setLstSubjectStudent(List<StudentSubjectStudy> lstSubjectStudent) {
            this.lstSubjectStudent = lstSubjectStudent;
        }
        
        public void setRowColour(int row, Color c){
            rowColours.set(row, c);
            fireTableRowsUpdated(row, row);
        }
        
        public Color getRowColour(int row){
            return rowColours.get(row);
        }
        
        @Override
        public int getRowCount() {
            return lstSubjectStudent.size();
        }

        @Override
        public int getColumnCount() {
            return columnName.length;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return colType[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == columnName.length -1){
                return true;
            }else return false;
        }
        
        

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return lstSubjectStudent.get(rowIndex).getIdSubject();
                case 1:
                    return lstSubjectStudent.get(rowIndex).getSubject().getName();
                case 2:
                    return lstSubjectStudent.get(rowIndex).getMark();
                default:
                    return null;
            }
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            System.out.println("Edit at " + rowIndex + ", " + columnIndex);
            
            int value = (Integer)aValue;
            // rowIndex added to indexChanges, (set of index which mark of row has been updated)
            if(lstSubjectStudent.get(rowIndex).getMark() != value){
                indexChanges.add(rowIndex);
                setRowColour(rowIndex, Color.blue);
            }
            lstSubjectStudent.get(rowIndex).setMark(value);
            
            fireTableCellUpdated(rowIndex, columnIndex);
            
            System.out.println("Set IndexChanges : " + indexChanges);
        }
        
        @Override
        public String getColumnName(int column) {
            return columnName[column];
        }

        public Set<Integer> getIndexChanges() {
            return indexChanges;
        }

        public void setIndexChanges(Set<Integer> indexChanges) {
            this.indexChanges = indexChanges;
        }
        
        
    }
    
}




